﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace WindowsFormsApp4
{
    public partial class Editfrom : Form
    {
        public Editfrom()
        {
            InitializeComponent();
        }


        public Editfrom(int id, string title, string author, string createdTime)
        {

            InitializeComponent();
            this.id = id;
            textBox1.Text = title;
            textBox2.Text = author;
            textBox3.Text = createdTime.ToString();
        }
        private int id;


        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }


        //保存
        private void button1_Click(object sender, EventArgs e)
        {
           

            var title = textBox1.Text;
             var author = textBox2.Text;
            

               var createdTimeString = textBox3.Text.ToString();

            var createdTime = createdTimeString.Count() > 0 ? int .Parse (createdTimeString) : 0;
            

            //更新
            if (id > 0)
            {
                if (!string .IsNullOrEmpty (title ))
                {
                    var sql = string.Format("update Articles set Title='{0}',Author='{1}',CreatedTime='{2}' where Id='{3}'", title, author, createdTime, this.id);
                    var res = DbHeper.AddOrUpdataDelete(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("更新成功");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            //添加
            else
            {
   


                if(!string .IsNullOrEmpty (title))
                {
                    var sql = string.Format("insert into Articles(Title,Author,CreatedTime)values('{0}','{1}','{2}')", title, author, createdTime);

                    var res = DbHeper.AddOrUpdataDelete(sql);

                    if (res==1)
                    {
                        MessageBox.Show("添加成功");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
/*                    else
                    {
                        MessageBox.Show("添加失败");
                        this.Close();
                    }*/

                }
                else
                {
                    MessageBox.Show("您未输入数据");
                }
            }


 
 


           

        } //取消
                private void button2_Click(object sender, EventArgs e)
                {
                    this.DialogResult = DialogResult.No;
                    this.Close();
                }
    } }
